package com.nowcoder.community.controller;

import com.nowcoder.community.entity.Event;
import com.nowcoder.community.entity.User;
import com.nowcoder.community.event.EventProducer;
import com.nowcoder.community.service.LikeService;
import com.nowcoder.community.util.CommunityConstant;
import com.nowcoder.community.util.CommunityUtil;
import com.nowcoder.community.util.HostHolder;
import com.nowcoder.community.util.RedisKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.web.PortResolverImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>Title: LikeController</p>
 * <p>author 12427</p>
 *
 * @version ver1.0
 * @date 2022/8/19 15:56
 */

@Controller
public class LikeController implements CommunityConstant {

    @Autowired
    private LikeService likeService ;

    @Autowired
    private HostHolder hostHolder ;

    @Autowired
    private EventProducer eventProducer ;


    @Autowired
    private RedisTemplate redisTemplate ;


    /**
     *
     * @param entityType
     * @param entityId
     * @param entityUserId  被点赞的人
     * @param postId
     * @return
     */
    @RequestMapping(path = "/like" , method = RequestMethod.POST)
    @ResponseBody
    public String like(int entityType , int entityId , int entityUserId  , int postId){
        User user = hostHolder.getUser() ;
        // 未来统一处理拦截

        // 点赞
        likeService.like(user.getId() , entityType , entityId , entityUserId);

        // 数量
        long likeCount = likeService.findEntityLikeCount(entityType , entityId) ;

        // 状态
        int likeStatus = likeService.findEntityLikeStatus(user.getId() , entityType , entityId) ;

        // 返回的结果
        Map<String, Object> map = new HashMap<>() ;
        map.put("likeCount" , likeCount) ;
        map.put("likeStatus" , likeStatus) ;


        // 触发点赞事件
        if(likeStatus == 1 ) {
            // 点赞才触发通知
            Event event = new Event()
                    .setTopic(TOPIC_LIKE)
                    .setUserId(hostHolder.getUser().getId())
                    .setEntityType(entityType)
                    .setEntityId(entityId)
                    .setEntityUserId(entityUserId)
                    .setData("postId" , postId) ;

            eventProducer.fireEvent(event);
        }

        if(entityType == ENTITY_TYPE_POST){
            // 计算帖子的分数
            String redisKey = RedisKeyUtil.getPostScoreKey() ;
            redisTemplate.opsForSet().add(redisKey , postId) ;

        }


        return CommunityUtil.getJSONString(0 , null , map) ;
    }



}
